package com.lanswon.qzsmk.controller.appManageController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.base.HttpContants;
import com.lanswon.qzsmk.controller.cardManageController.BaseController;
import com.lanswon.qzsmk.model.AppClassification;
import com.lanswon.qzsmk.model.AppKind;
import com.lanswon.qzsmk.model.CardProdunctDefinition;
import com.lanswon.qzsmk.service.appManageService.AppFeeService;
import com.lanswon.qzsmk.service.systemManageService.ParamManageService;
import com.lanswon.qzsmk.vo.CancelCardInfo;
import com.lanswon.qzsmk.vo.FeeRulesVo;
import com.lanswon.qzsmk.vo.ResultObject;
import com.lanswon.qzsmk.vo.ResultVo;

@Controller
@RequestMapping(value="/appChargeDef")
public class AppChargeDefController{
	
	@Autowired
	AppFeeService afs;
	
	@Autowired
	ParamManageService pm;
	
	
	@RequiresPermissions("appChargeDef:index")
	@RequestMapping(value="index",name="应用收费定义,appChargeDef:index",method={RequestMethod.GET,RequestMethod.POST})
	public String goAppChargeDef(){
		return "AppChargeDef";
	}
	
	@RequiresPermissions("appChargeDef:showBaseInfo")
	@SuppressWarnings("unchecked")
	@RequestMapping(value="showBaseInfo",method=RequestMethod.POST,name="显示页面基础数据,appChargeDef:showBaseInfo")
	@ResponseBody
	public Map<String, Object> showBaseInfo(){
		Map<String, Object> map=new HashMap<>();
		List<CardProdunctDefinition> cdProDefinitions=null;
		ResultVo resultVo=pm.queryAllCardProdunctDefinition();//卡产品集合
		if(resultVo.getResultStatus().getCode()==HttpContants.RETURN_SUCCESS){
			cdProDefinitions=(List<CardProdunctDefinition>)((ResultObject)resultVo.getServiceObject()).getItems();
		}
		List<AppClassification> AppClassifications=pm.queryAppClassification();
		List<AppKind> appKinds =pm.queryAppKind();
		map.put("cdProDefinitions", cdProDefinitions);
		map.put("appClassifications", AppClassifications);
		map.put("appKinds", appKinds);
		return map;
	}
	
	/**
	 * 动态显示应用类型
	 * @param appKindId
	 * @return
	 */
	@RequiresPermissions("appChargeDef:showAppTypeByAppKindId")
	@RequestMapping(value="/showAppTypeByAppKindId",name="根据appKingdID查询应用,appChargeDef:showAppTypeByAppKindId",method=RequestMethod.POST)
	@ResponseBody
	public List<AppKind> showAppTypeByAppKindId(String appKindId){
		return pm.queryAppKindByKindID(appKindId);
	}
	
	
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequiresPermissions("appChargeDef:showAppFeeList")
	@RequestMapping(value="/showAppFeeList",name="显示收费定义列表,appChargeDef:showAppFeeList",method=RequestMethod.POST)
	public DwzPage<FeeRulesVo> showAppFeeList(FeeRulesVo vo,Integer pageNum,Integer numPerPage){
		DwzPage<FeeRulesVo> dPage =null;
//		if(vo.getNumPerPage()==null){//加载页面时执行
//			vo.setCurrentPage(1);
//			vo.setNumPerPage(20);
//		}else {
//			if(vo.getCurrentPage()<=1||pageCount==0){
//				vo.setCurrentPage(1);
//			}else if(vo.getCurrentPage()>=pageCount&&pageCount!=0){
//				vo.setCurrentPage(pageCount);
//			}
//		}
		if(pageNum==null&&numPerPage==null){
			pageNum=1;
			numPerPage=20;			
		}
		vo.setCurrentPage(pageNum);
		vo.setNumPerPage(numPerPage);
		vo.setStartLine((vo.getCurrentPage()-1)*vo.getNumPerPage());
		ResultVo resultVo=afs.showAppFeeList(vo);
		if(resultVo.getResultStatus().getCode()==HttpContants.RETURN_SUCCESS){
			dPage=(DwzPage<FeeRulesVo>) resultVo.getServiceObject();
		}
		return dPage;
	}
	
	@RequiresPermissions("appChargeDef:goAddChargeDef")
	@RequestMapping(value="/goAddChargeDef",name="链接新增应用收费定义,appChargeDef:goAddChargeDef",method={RequestMethod.POST,RequestMethod.GET})
	public String goAddChargeDef(String param,HttpServletRequest request){
		if(StringUtils.isNotEmpty(param)){
			FeeRulesVo vo=afs.queryFeeByFeeRulesId(param);
			request.getSession().setAttribute("vo", vo);
		}	
		return "AddChargeDef";
	}
	
	@RequiresPermissions("appChargeDef:getChargeDefInfo")
	@ResponseBody
	@RequestMapping(value="/getChargeDefInfo",name="获取修改信息,appChargeDef:getChargeDefInfo",method=RequestMethod.POST)
	public FeeRulesVo getChargeDefInfo(HttpServletRequest request){
		FeeRulesVo vo=(FeeRulesVo) request.getSession().getAttribute("vo");
		request.getSession().removeAttribute("vo");
		return vo;
	}
	
	@RequiresPermissions("appChargeDef:addOrUpdateChargeDef")
	@ResponseBody
	@RequestMapping(value="/addOrUpdateChargeDef",name="新增或修改收费定义,appChargeDef:addOrUpdateChargeDef",method=RequestMethod.POST)
	public Map<String, Object> addOrUpdateChargeDef(FeeRulesVo vo,String flag){
		Map<String, Object> map=new HashMap<>();
		ResultVo resultVo=null;
		if(flag.equals("0")){
			resultVo=afs.addAppFeeRule(vo);
		}else{
			resultVo=afs.modifyAppFeeRule(vo);
		}
		if(resultVo.getResultStatus().getCode()==HttpContants.RETURN_ERROR){
			map.put("statusCode", "300");
			map.put("statusCode", resultVo.getResultStatus().getMessage());
		}else if(resultVo.getResultStatus().getCode()==HttpContants.RETURN_SUCCESS){
			map.put("statusCode", "200");
			map.put("message",  resultVo.getResultStatus().getMessage());
			map.put("navTabId", "appChargeDef");
			map.put("callbackType", "closeCurrent");
			map.put("forwardUrl", "appChargeDef/index");//post请求
		}
		return map;
		
	}
}
